package com.bloveoacom.base;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.bloveoacom.util.Constant;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.weichi.BuildConfig;
import com.bloveoacom.weichi.bean.UserBean;

import static com.bloveoacom.base.AppConfig.USERINFO;


/**
 * Created by yujunlong on 2017/4/18.
 */

public class UserInfoBean {
    private static UserInfoBean userInfoBean = null;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private boolean isLogin;//是否已经登录
    private boolean isFirstOpen;//是否第一次打开app



    private String uid;//用户id
    private String token;//用户token
    private String phone;
    private String header;
    private int userType;
    private String nickName;
    private String privateKey;
    private String pubilcKey;
    private String walletAccount;
    private String isSign;
    private String invitationCode;
    private String signature;
    private String balanceNum;




    // 实现了线程安全，又避免了同步带来的性能影响。
    public static UserInfoBean getInstance() {
        if (userInfoBean == null) {
            synchronized (UserInfoBean.class) {
                if (userInfoBean == null) {
                    userInfoBean = new UserInfoBean(AppContext.context());
                }
            }
        }
        return userInfoBean;
    }

    private UserInfoBean(Context context) {
        super();
        init(context);
    }

    public void init(Context context) {
        sp = context.getSharedPreferences(USERINFO, Activity.MODE_PRIVATE);
        editor = sp.edit();
    }
    /**
     * 删除帐号信息
     *
     */
    public void clearUserInfo() {
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
        //清空信息以后数据就为默认，需要设置是否第一次打开
        setFirstOpen(isFirstOpen);
        getAccess();
    }



    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
        editor.putString(Constant.PHONE, phone);
        editor.commit();
    }

    public int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
        editor.putInt(Constant.USERTYPE, userType);
        editor.commit();
    }


    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
        editor.putString(Constant.NICKNAME, nickName);
        editor.commit();
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
        editor.putString(Constant.HEADIMG, header);
        editor.commit();
    }

    //====================================================
    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
        editor.putString(Constant.TOKEN, token);
        editor.commit();
    }
    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
        editor.putString(Constant.USERID, uid);
        editor.commit();
    }

    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
        editor.putBoolean(Constant.ISLOGIN, login);
        editor.commit();
    }


    public boolean isFirstOpen() {
        return isFirstOpen;
    }

    public void setFirstOpen(boolean firstOpen) {
        isFirstOpen = firstOpen;
        editor.putBoolean(Constant.FIRSTOPEN, firstOpen);
        editor.commit();
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
        editor.putString(Constant.PRIVATEKEY, privateKey);
        editor.commit();
    }

    public String getPubilcKey() {
        return pubilcKey;
    }

    public void setPubilcKey(String pubilcKey) {
        this.pubilcKey = pubilcKey;
        editor.putString(Constant.PUBILCKEY, pubilcKey);
        editor.commit();
    }

    public String getWalletAccount() {
        return walletAccount;
    }

    public void setWalletAccount(String walletAccount) {
        this.walletAccount = walletAccount;
        editor.putString(Constant.WALLETACCOUNT, walletAccount);
        editor.commit();
    }

    public String getIsSign() {
        return isSign;
    }

    public void setIsSign(String isSign) {
        this.isSign = isSign;
        editor.putString(Constant.ISSIGN, isSign);
        editor.commit();
    }

    public String getInvitationCode() {
        return invitationCode;
    }

    public void setInvitationCode(String invitationCode) {
        this.invitationCode = invitationCode;
        editor.putString(Constant.INVITATIONCODE, invitationCode);
        editor.commit();
    }

    public String getSignature() {
        return signature == null ? "" : signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
        editor.putString(Constant.SIGNATURE, signature);
        editor.commit();
    }

    public String getBalanceNum() {
        return balanceNum == null ? "" : balanceNum;
    }

    public void setBalanceNum(String balanceNum) {
        this.balanceNum = balanceNum;
        editor.putString(Constant.BALANCENUM, balanceNum);
        editor.commit();
    }
    //================================================

    //数据共享
    public void setKV(String key, String value) {
        editor.putString(BuildConfig.APPLICATION_ID+key, value);
        editor.commit();
    }

    public String getValue(String key) {
        return sp.getString(BuildConfig.APPLICATION_ID+key, "");
    }

    /***
     * 读取配置文件，并赋值
     */
    public void getAccess() {
        isLogin = sp.getBoolean(Constant.ISLOGIN, false);
        uid = sp.getString(Constant.USERID, "0");
        token = sp.getString(Constant.TOKEN, "");
        phone = sp.getString(Constant.PHONE, "");
        userType = sp.getInt(Constant.USERTYPE,1);
        nickName = sp.getString(Constant.NICKNAME,"");
        isFirstOpen  = sp.getBoolean(Constant.FIRSTOPEN,true);
        header = sp.getString(Constant.HEADIMG, "");
        walletAccount = sp.getString(Constant.WALLETACCOUNT, "");
        privateKey = sp.getString(Constant.PRIVATEKEY, "");
        pubilcKey = sp.getString(Constant.PUBILCKEY, "");
        isSign = sp.getString(Constant.ISSIGN, "0");
        invitationCode = sp.getString(Constant.INVITATIONCODE, "");
        signature = sp.getString(Constant.SIGNATURE, "");
        balanceNum = sp.getString(Constant.BALANCENUM, "");

    }

    /***
     * 写入登录信息配置文件
     */
    public void setAccess(UserBean bean) {
        editor.putString(Constant.USERID, bean.userId);
        editor.putString(Constant.PHONE, bean.phone);
        if(!StringUtils.isEmpty(bean.token)){
            editor.putString(Constant.TOKEN, bean.token);
        }
        editor.putString(Constant.NICKNAME, bean.name);
        editor.putString(Constant.HEADIMG, bean.header);
        editor.putString(Constant.WALLETACCOUNT, bean.walletAccount);
        editor.putString(Constant.PUBILCKEY, bean.publicKey);
        editor.putString(Constant.PRIVATEKEY, bean.privateKey);
        editor.putString(Constant.ISSIGN, bean.isSign);
        editor.putString(Constant.INVITATIONCODE, bean.invitationCode);
        editor.putString(Constant.SIGNATURE, bean.signature);
        editor.putString(Constant.BALANCENUM, bean.balanceNum);
        editor.commit();
        getAccess();

    }


}
